#!/bin/bash

# Flutter构建GUI脚本 - 适用于Mac双击执行
# 用法: 双击此文件即可启动图形化构建界面

# 切换到脚本所在目录的上级目录（项目根目录）
cd "$(dirname "$0")/.."

# 检查是否在正确的Flutter项目目录
if [ ! -f "pubspec.yaml" ]; then
    echo "❌ 错误: 未找到pubspec.yaml文件"
    echo "请确保此脚本位于Flutter项目的scripts目录中"
    read -p "按回车键退出..."
    exit 1
fi

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
CYAN='\033[0;36m'
NC='\033[0m' # No Color

# 显示欢迎信息
clear
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
echo -e "${BLUE}             🚀 Flutter应用构建工具 🚀                    ${NC}"
echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
echo ""

# 获取项目信息
APP_NAME="玩真的5"
VERSION=$(grep "^version:" pubspec.yaml | cut -d' ' -f2 | cut -d'+' -f1)
BUILD_NUMBER=$(grep "^version:" pubspec.yaml | cut -d' ' -f2 | cut -d'+' -f2)
PACKAGE_NAME="com.wanzhende.true5"

echo -e "${CYAN}📱 项目信息:${NC}"
echo -e "   应用名称: ${GREEN}$APP_NAME${NC}"
echo -e "   版本号: ${GREEN}$VERSION${NC}"
echo -e "   构建号: ${GREEN}$BUILD_NUMBER${NC}"
echo -e "   包名: ${GREEN}$PACKAGE_NAME${NC}"
echo ""

# 主菜单函数
show_main_menu() {
    echo -e "${YELLOW}请选择操作:${NC}"
    echo ""
    echo -e "${CYAN}📦 构建选项:${NC}"
    echo "  1) 🤖 构建Android APK (Release)"
    echo "  2) 🤖 构建Android APK (Debug)" 
    echo "  3) 🌐 构建Web应用 (Release)"
    echo "  4) 🪟 构建Windows应用 (Release) - 需要Windows环境"
    echo "  5) 🍏 构建macOS应用 (Release)"
    echo ""
    echo -e "${CYAN}🔄 版本管理:${NC}"
    echo "  6) 📈 升级版本号"
    echo "  7) 📊 查看版本信息"
    echo ""
    echo -e "${CYAN}🛠️ 其他选项:${NC}"
    echo "  8) 🔍 检查Flutter环境"
    echo "  9) 📁 打开构建输出目录"
    echo "  0) ❌ 退出"
    echo ""
    echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
}

# 版本升级菜单
show_version_menu() {
    echo ""
    echo -e "${YELLOW}版本升级选项:${NC}"
    echo "  1) 升级补丁版本 ($VERSION → $(echo $VERSION | awk -F. '{print $1"."$2"."($3+1)}'))"
    echo "  2) 升级次版本 ($VERSION → $(echo $VERSION | awk -F. '{print $1"."($2+1)".0"}'))"
    echo "  3) 升级主版本 ($VERSION → $(echo $VERSION | awk -F. '{print ($1+1)".0.0"}'))"
    echo "  4) 升级构建号 ($BUILD_NUMBER → $(($BUILD_NUMBER + 1)))"
    echo "  0) 返回主菜单"
    echo ""
}

# 执行构建
execute_build() {
    local platform=$1
    local build_type=$2
    
    echo ""
    echo -e "${GREEN}🚀 开始构建 $platform ($build_type)...${NC}"
    echo ""
    
    # 执行构建脚本
    chmod +x scripts/build_with_version.sh
    ./scripts/build_with_version.sh "$platform" "$build_type"
    
    if [ $? -eq 0 ]; then
        echo ""
        echo -e "${GREEN}✅ 构建成功完成！${NC}"
        echo ""
        read -p "按回车键继续..."
    else
        echo ""
        echo -e "${RED}❌ 构建失败！${NC}"
        echo ""
        read -p "按回车键继续..."
    fi
}

# 检查Flutter环境
check_flutter_env() {
    echo ""
    echo -e "${CYAN}🔍 检查Flutter环境...${NC}"
    echo ""
    
    flutter doctor -v
    
    echo ""
    read -p "按回车键继续..."
}

# 打开构建输出目录
open_builds_dir() {
    if [ -d "builds" ]; then
        open builds/
        echo ""
        echo -e "${GREEN}✅ 已打开构建输出目录${NC}"
    else
        echo ""
        echo -e "${YELLOW}⚠️  构建输出目录不存在，请先进行构建${NC}"
    fi
    echo ""
    read -p "按回车键继续..."
}

# 升级版本
upgrade_version() {
    local type=$1
    echo ""
    echo -e "${CYAN}📈 升级版本号...${NC}"
    chmod +x scripts/version_manager.sh
    ./scripts/version_manager.sh bump "$type"
    
    if [ $? -eq 0 ]; then
        # 重新获取版本信息
        VERSION=$(grep "^version:" pubspec.yaml | cut -d' ' -f2 | cut -d'+' -f1)
        BUILD_NUMBER=$(grep "^version:" pubspec.yaml | cut -d' ' -f2 | cut -d'+' -f2)
        echo -e "${GREEN}✅ 版本升级成功！新版本: $VERSION+$BUILD_NUMBER${NC}"
    else
        echo -e "${RED}❌ 版本升级失败！${NC}"
    fi
    echo ""
    read -p "按回车键继续..."
}

# 查看版本信息
show_version_info() {
    echo ""
    echo -e "${CYAN}📊 版本信息:${NC}"
    chmod +x scripts/version_manager.sh
    ./scripts/version_manager.sh show
    echo ""
    read -p "按回车键继续..."
}

# 主循环
while true; do
    clear
    echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
    echo -e "${BLUE}             🚀 Flutter应用构建工具 🚀                    ${NC}"
    echo -e "${BLUE}═══════════════════════════════════════════════════════════${NC}"
    echo ""
    echo -e "${CYAN}📱 当前版本: ${GREEN}$VERSION+$BUILD_NUMBER${NC}"
    echo ""
    
    show_main_menu
    
    read -p "请输入选项 (0-9): " choice
    
    case $choice in
        1)
            execute_build "android" "release"
            ;;
        2)
            execute_build "android" "debug"
            ;;
        3)
            execute_build "web" "release" 
            ;;
        4)
            execute_build "windows" "release"
            ;;
        5)
            execute_build "macos" "release"
            ;;
        6)
            while true; do
                clear
                echo -e "${CYAN}📈 版本管理 - 当前版本: ${GREEN}$VERSION+$BUILD_NUMBER${NC}"
                show_version_menu
                read -p "请输入选项 (0-4): " version_choice
                
                case $version_choice in
                    1)
                        upgrade_version "patch"
                        break
                        ;;
                    2)
                        upgrade_version "minor"
                        break
                        ;;
                    3)
                        upgrade_version "major"
                        break
                        ;;
                    4)
                        upgrade_version "build"
                        break
                        ;;
                    0)
                        break
                        ;;
                    *)
                        echo -e "${RED}❌ 无效选项，请重新选择${NC}"
                        sleep 1
                        ;;
                esac
            done
            ;;
        7)
            show_version_info
            ;;
        8)
            check_flutter_env
            ;;
        9)
            open_builds_dir
            ;;
        0)
            echo ""
            echo -e "${GREEN}👋 感谢使用Flutter构建工具！${NC}"
            echo ""
            exit 0
            ;;
        *)
            echo ""
            echo -e "${RED}❌ 无效选项，请重新选择${NC}"
            sleep 1
            ;;
    esac
done